# 命令行输入
# input([prompt]) 
# str = input('sss:')
# print('ddd:', str)

# 打开文件
# file object = open(file_name [, access_mode][, buffering])
fo = open('foo.txt', 'w')
print('文件名', fo.name)
print('是否已关闭', fo.closed)
print('访问模式', fo.mode)

# 关闭文件
# fileObject.close()
fo.close()
print('是否已关闭', fo.closed)

# 写文件
# fileObject.write(string)
fo = open("foo.txt", "a")
fo.write( "124444434\n")
fo.flush()
fo.close()

# 读文件
# fileObject.read([count])
fo = open("foo.txt", "r+")
s = fo.read(3)
print("读取的字符串是 : ", s)
# 关闭打开的文件
fo.close()

# with打开文件 出缩进后自动关闭文件
with open('foo.txt', 'r+') as fo:
    # fo.write('\n456')
    print(fo.read(4))
print(fo.closed)

# 输出列表
test_list = [100,200,300,400]
with open('foo.txt', 'a') as fo:
    fo.writelines([str(x)+'\n' for x in test_list])

# 光标移动
with open('foo.txt', 'r') as fo:
    fo.seek(4)  # 光标移动
    t = fo.read(4)
    print(t)

# 读取全部信息
with open('foo.txt', 'r') as fo:
    t = fo.read()
    print(t)

# 读取一行
with open('foo.txt', 'r') as fo:
    line = 0
    while True:
        line += 1
        t = fo.readline()
        if t == '':
            break
        print(line, t, end='\n')

# 读取全部行 如添加参数只输出第一行
with open('foo.txt', 'r') as fo:
    t = fo.readlines()
    print(t)